//author:jxx
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
import gridBody from '../../views/mall/goodstytemchilren/Mall_campphotoChild.vue'
let extension = {
    components: {//动态扩充组件或组件路径
        //表单header、content、footer对应位置扩充的组件
        gridHeader: '',//{ template: "<div>扩展组xx件</div>" },
        gridBody: gridBody,
        gridFooter: '',
        //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
        modelHeader: '',
        modelBody: '',
        modelFooter: ''
    },
    buttons: { view: [], box: [], detail: [] },//扩展的按钮
    methods: {//事件扩展
        getFormOption (field) {
            let option;
            this.editFormOptions.forEach((x) => {
                x.forEach((item) => {
                    if (item.field == field) {
                        option = item;
                    }
                });
            });
            return option;
        },
        
        onInit () {
            let checkstr = new Object();
            this.boxOptions.width = 700;
            this.boxOptions.height = 500;
            let campsiteId = this.getFormOption('campsiteId')
            let campsitePhaseId = this.getFormOption('campsitePhaseId')
            // 用户查看权限
            let isvisibility = this.getFormOption('isvisibility')
            // 团队和租
            let campteam = this.getFormOption('campgroupings')
            let campgrouping = this.getFormOption('campgrouping')
            campsiteId.onChange = (n) => {
                // console.log(n)
                this.getCamper(n)
                let parmas = {
                    order: "desc",
                    page: 1,
                    rows: 100,
                    sort: "phase_id_fk",
                    wheres: `[{"name":"campsite_id_fk","value":"${n}","displayType":"text"}]`,
                };
                this.http.post("/api/CampsiteHase/getPageData", parmas).then((res) => {
                    campsitePhaseId.data = res.rows.map((i) => {
                    checkstr = i.campgrouping;
                    // console.log(checkstr)
                        return {
                            key: i.phase_id_fk,
                            value: i.phase_name,
                        };
                    });
                    if(checkstr){
                        let checkobj = JSON.parse(checkstr)
                        console.log(checkobj)
                            campteam.data = checkobj.map((i) => {
                        return {
                            key: i.id,
                            value: i.name,
                            group:i.group,
                        };
                    });
                }
                });
            }
            isvisibility.onChange = (n) => {
               
                if(n==1){  //团队可见
                    campteam.hidden=false 
                    campgrouping.hidden=true 
                }else{
                    campteam.hidden=true
                    campgrouping.hidden=true 
                }
                console.log(campteam.hidden)
            }
            campteam.onChange = (n,item) => {
                console.log(n,item)
                let group = item.data[0].group
                if(n==1){  //组可见
                    campgrouping.hidden=false 
                    campgrouping.data = group.map((i) => {
                        return {
                            key: i.id,
                            value: i.name,
                        };
                    });
                }else{
                    campgrouping.hidden=true 
                }
                console.log(campteam.hidden)
            }
            let parmas2 = {
                order: "desc",
                page: 1,
                rows: 100,
                sort: "phase_id_fk",
                wheres: `[]`,
            };
            let list = []
            this.http.post("/api/CampsiteHase/getPageData", parmas2).then((res) => {
                list = res.rows.map((i) => {
                    return {
                        key: i.phase_id_fk,
                        value: i.phase_name,
                    };
                });
                this.columns.forEach(i => {
                    if (i.field == 'campsitePhaseId') {
                        i.bind.data = list
                    }
                })
            });
            this.columns.push({
                title: '操作',
                fixed: 'right',
                width: 80,
                render: (h, { row, column, index }) => {
                    return h(
                        "div", { style: {} }, [
                        
                       
                         h(
                          "a", {
                              props: {},
                              style: {},
                              on: {
                                  click: (e) => {
                                    // console.log(this.$refs.gridHeader)
                                    this.$refs.gridBody.open6(row);
                                  }
                              }
                          },
                          "    下载"
                      ),
                      // h(
                      //   "a", {
                      //       props: {},
                      //       style: {},
                      //       on: {
                      //           click: (e) => {
                      //           //   this.$refs.gridHeader.open8(row);
                      //             // this.linkData(row);
                      //           }
                      //       }
                      //   },
                      //   "    发送"
                    // ),
                        ])
                }
            })
        },
        addBefore (formData) { //新建保存前formData为对象，包括明细表
            return this.veriFromTable(formData)
          },
          getCamper(row){
            let camppeople = this.getFormOption('camppeople')
            let parmas = {
                order: "desc",
                page: 1,
                rows: 100,
                sort: "phase_id_fk",
                // wheres: `[{\"name\":\"phase_id_fk\",\"value\":\"${this.editFormFields.campsitePhaseId}\",\"displayType\":\"=\"},{\"name\":\"campsite_id_fk\",\"value\":\"${row.campsiteId}\",\"displayType\":\"=\",\"name\":\"offered\",\"value\":\"1\",\"name\":\"campsite_type\",\"value\":\"1\",}]`,
                wheres: `[{\"name\":\"phase_id_fk\",\"value\":\"${this.editFormFields.campsitePhaseId}\",\"displayType\":\"checkbox\",\"name\":\"campsite_id_fk\",\"value\":\"${row}\",\"displayType\":\"=\",\"name\":\"offered\",\"value\":\"1\",\"name\":\"campsite_type\",\"value\":\"1\"}]`,
            };
            this.http.post("/api/V_campphase_people/getPageData", parmas).then((res) => {
                let  option=this.$refs.table.getSelected()[0];
                if(option && option.camppeople){
                    let arr=JSON.parse(JSON.stringify(option.camppeople.split(",")))
                    this.editFormFields.camppeople = arr.map(i => i * 1)
                    // this.editFormFields.camppeople = ["22","24",]
                }
                // let aaa=[22,24,]
                // console.log( JSON.parse(JSON.stringify(option.camppeople.split(","))))
                // console.log(aaa)
                camppeople.data = res.rows.map((i) => {
                        return {
                            key: i.camppeoid,
                            value: i.name,
                        };
                    });
            });
          },    
          beginEdit(row, column, index) {
            console.log("编辑开始前" + JSON.stringify(row));
            return true;
          },
          updateBefore (formData) { //编辑保存前formData为对象，包括明细表、删除行的Id
            return this.veriFromTable(formData)
          },
          // 保存前编辑  
          veriFromTable (formData) {
            let campteam = this.getFormOption('campgroupings')
            let campteamss = this.getFormOption('campgrouping')
            // 将从表中的日期切分为开始日期 和结束日期
            console.log(formData, "formData")
            console.log(campteam.data)
            formData.mainData.campgrouping=JSON.stringify(campteamss.data)
            return true
          },
        rowClick ({ row, column, event }) {
            this.$refs.table.$refs.table.toggleRowSelection(row);
            return true;
        },
        modelOpenAfter (row) {  //点击编辑/新建按钮弹出框后，可以在此处写逻辑，如，从后台获取数据
            let isEDIT = this.currentAction == this.const.EDIT;
            let tableobj =this.$refs.table.getSelected()[0]
            console.log(tableobj)
            let  checkstr=""
            console.log()
            if(isEDIT){   //编辑
                let campsitePhaseId = this.getFormOption('campsitePhaseId')
                let campteam = this.getFormOption('campgroupings')
                let campgrouping = this.getFormOption('campgrouping')
                if(tableobj.isvisibility==1){
                    campteam.hidden=false 
                    campgrouping.hidden=false 
                }
                let parmas = {
                    order: "desc",
                    page: 1,
                    rows: 100,
                    sort: "phase_id_fk",
                    wheres: `[{"name":"campsite_id_fk","value":"${row.campsiteId}","displayType":"text"}]`,
                };
                this.http.post("/api/CampsiteHase/getPageData", parmas).then((res) => {
                    this.editFormFields.campsitePhaseId = row.campsitePhaseId
                    // this.editFormFields.campgrouping = ""
                    campsitePhaseId.data = res.rows.map((i) => {
                      checkstr = i.campgrouping;
                        // console.log(checkstr)
                            return {
                                key: i.phase_id_fk,
                                value: i.phase_name,
                            };
                        });
                        if(checkstr){
                            let checkobj = JSON.parse(checkstr)
                            // console.log(checkobj)
                            campgrouping.data = checkobj[0].group.map((i) => {
                            return {
                                key: i.id,
                                value: i.name,
                            };
                        });
                        campteam.data = checkobj.map((i) => {
                            return {
                                key: i.id,
                                value: i.name,
                            };
                        });
                    }
                    
                });
               
                this.getCamper(row.campsiteId)
            }
            
        },
    }
};
export default extension;